package controller;

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.EventDAO;
import model.UserDAO;
import model.VenueDAO;
import beans.EventBean;
import beans.UserBean;
import beans.VenueBean;

/**
 * Servlet implementation class EventCreationServlet
 */
@WebServlet("/EventCreationServlet")
public class EventCreationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EventCreationServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{	
			HttpSession session = request.getSession();
			EventBean event = new EventBean();
			String Start = request.getParameter("year") + "-" + request.getParameter("month") + "-" + request.getParameter("day") + " " + request.getParameter("starttime") + ":00.0";
			ArrayList<EventBean> usersEvent = (ArrayList<EventBean>)session.getAttribute("usersevent");
			if (request.getParameter("editing").equals("no"))
			for (EventBean eventt : usersEvent) {
				System.out.println(eventt.getStartTimeStamp().toString() + "---" + Start);
				if (eventt.getStartTimeStamp().toString().equals(Start)) {
					String fail = "You can't create an event if you are already hosting or attending another one at the same time! Please specify a different date and time.";
					session.setAttribute("eventcreationfail", fail);
					response.sendRedirect("eventcreation.jsp");
					return;
				}
				Date now = new Date(System.currentTimeMillis());
				if (now.after(Timestamp.valueOf(Start))) {
					String fail = "You must pick a future date.";
					session.setAttribute("eventcreationfail", fail);
					response.sendRedirect("eventcreation.jsp");
					return;
				}
			}
			VenueBean venue = new VenueBean(request.getParameter("venue"), request.getParameter("venueaddress"), request.getParameter("city"), request.getParameter("region"), request.getParameter("country"));
			Integer venueid = Integer.valueOf(request.getParameter("venueid"));
			System.out.println("#####VENUEID" + venueid);
			if (venueid != 0)
				venue.setId(venueid);
			/*if (request.getParameter("newvenue").equals("true"))
				venue = new VenueBean(request.getParameter("venue"), request.getParameter("venueaddress"), request.getParameter("country"), request.getParameter("region"), request.getParameter("city"));
			else
				venue = new VenueBean(new Integer(request.getParameter("existingvenueid")));*/
			UserBean user = (UserBean)session.getAttribute("currentSessionUser");
			if (request.getParameter("editing").equals("yes")) {
				EventBean oldevent = (EventBean)session.getAttribute("currentEvent");
				event.setHost(oldevent.getHost());
			} else
				event.setHost(user.getEmail());
			event.setName(request.getParameter("title"));
			event.setDescription(request.getParameter("description"));

			String End;
			Integer dayplus = new Integer(request.getParameter("day"));
			dayplus++;
			String daypluss;
			if (dayplus < 10)
				daypluss = "0" + dayplus;
			else
				daypluss = dayplus.toString();
			if (request.getParameter("endtime").compareTo(request.getParameter("starttime")) < 0)
				End = request.getParameter("year") + "-" + request.getParameter("month") + "-" + daypluss + " " + request.getParameter("starttime") + ":00";
			else
				End = request.getParameter("year") + "-" + request.getParameter("month") + "-" + request.getParameter("day") + " " + request.getParameter("endtime") + ":00";
			//System.out.println(Start);
			//System.out.println("PROVA####" + Timestamp.valueOf(Start));
			//System.out.println(End);
			//System.out.println("PROVA####" + Timestamp.valueOf(End));
			event.setStartTimeStamp(Timestamp.valueOf(Start));
			event.setEndTimeStamp(Timestamp.valueOf(End));
			event.setType(request.getParameter("type"));
			Integer participants = new Integer(request.getParameter("partnumber"));
			event.setParticipants(participants);
			Integer guests = new Integer(request.getParameter("guestnumber"));
			event.setGuests(guests);
			event.setDescription(request.getParameter("description"));
			if (venueid == 0) {
				event.setVenue(VenueDAO.createVenue(venue));
				event.setExistingVenue(false);
			} else {
				event.setVenue(venue);
				event.setExistingVenue(true);
			}	
			if (request.getParameter("editing").equals("yes")) {
				EventBean oldevent = (EventBean)session.getAttribute("currentEvent");
				EventDAO.editEvent(oldevent, event);
			}
			else
				EventDAO.createEvent(event);
			String start = event.getStartTimeStamp().toString();
			start = start.substring(0, 19);
			start = start.replace("-", "");
			start = start.replace(":", "");
			start = start.replace(" ", "");
			System.out.println(start);
			session.setAttribute("eventcreationfail", null);
			response.sendRedirect("EventServlet?host=" + event.getHost() + "&start=" + start);
		}




		catch (Throwable theException) 	    
		{
			System.out.println(theException); 
		}	
	}


}
